home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 285_03 / symbol.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-07-08  |  1.5 KB  |  61 lines

  1. /*--------------------------------------------------------------------------
  2. **        file:         SYMBOL.C
  3. **        project:    hoc3, "Higher Order Calculator"
  4. */
  5.  
  6. #include <stdio.h>
  7. #include <malloc.h>
  8.  
  9. #include "hocdecl.h"
  10.  
  11. static Symbol *symlist = (Symbol *) 0 ;
  12.  
  13. /*--------------------------------------------------------------------------
  14. **    LOOKUP    -    find symbol in table
  15. */
  16. Symbol *lookup( char *s)      /* find s in symbol table */
  17. {
  18.  Symbol *sp ;
  19.  
  20.    for (sp = symlist ; sp != (Symbol *) 0 ; sp = sp->next)
  21.       if (strcmp( sp->name, s) == 0)
  22.          return sp ;                /* found  */
  23.  
  24.    return (Symbol *) 0 ;            /* symbol not found */
  25. }
  26.  
  27. /*--------------------------------------------------------------------------
  28. **    INSTALL    -    install symbol in table
  29. */
  30. Symbol *install( char *s, int t, double d)    /* install s in symbol table */
  31. {
  32.  Symbol *sp ;
  33.  
  34.      sp = (Symbol *) emalloc( sizeof( Symbol)) ;
  35.  
  36.     sp->name = emalloc( strlen(s) + 1) ;
  37.     strcpy( sp->name, s) ;
  38.  
  39.     sp->type = t ;
  40.     sp->u.val = d ;
  41.     sp->next = symlist ;        /* add symbol to front of list */
  42.     symlist = sp ;
  43.  
  44.     return sp ;
  45. }
  46.  
  47. /*--------------------------------------------------------------------------
  48. **    EMALLOC    -    allocate memory for string, complain on error.
  49. */
  50. char *emalloc( unsigned n)
  51. {
  52.  char *p ;
  53.  
  54.    p = (char *) malloc( n) ;
  55.    if (p == (char *)0)
  56.       execerror("out of memory", (char *) 0) ;
  57.    return p ;
  58. }
  59.  
  60. /* end of symbol.c for HOC3.y */
  61.